package com.woniuxy.pxxt.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.pxxt.common.result.Result;
import com.woniuxy.pxxt.common.vo.CourseEditVo;
import com.woniuxy.pxxt.common.vo.CourseManageVo;
import com.woniuxy.pxxt.common.vo.FileVo;
import com.woniuxy.pxxt.dao.CourseDao;
import com.woniuxy.pxxt.dto.*;
import com.woniuxy.pxxt.entity.Course;
import com.woniuxy.pxxt.service.CourseService;
import com.woniuxy.pxxt.utils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 是培训项目下的阶段(Course)表服务实现类
 *
 * @author makejava
 * @since 2022-03-07 16:28:52
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired(required = false)
    CourseDao courseDao;

    @Autowired
    private QiniuUtil qiniuUtil;

    @Override
    public Result queryByPage(int currentPage, int pageSize, CourseManageVo courseManageVo) {
        PageHelper.startPage(currentPage, pageSize);
        List<CourseManagerDto> courseList = courseDao.queryViewAll(courseManageVo);
        PageInfo<CourseManagerDto> of = PageInfo.of(courseList);
        return Result.ok(of).message("查询成功");
    }

    @Override
    public Result queryByPage(int trainId, int currentPage, int pageSize, Course course) {
        PageHelper.startPage(currentPage, pageSize);
        List<CourseManagerDto> courseList = courseDao.queryAllByTrainId(trainId,course);
        PageInfo<CourseManagerDto> of = PageInfo.of(courseList);
        return Result.ok(of).message("查询成功");
    }

    @Override
    public Result queryById(Integer id) {
        CourseEditVo courseEditVo = courseDao.queryById(id);
        courseEditVo.setFileDomain(qiniuUtil.getFileDomain());
        courseEditVo.setFiles(courseDao.findCourseFile(id));
        return Result.ok(courseEditVo).message("查询成功");
    }

    @Transactional
    @Override
    public Result insert(CourseEditVo courseEditVo) {
        System.out.println(courseEditVo);
        //  课程添加
        if (courseDao.insert(courseEditVo) > 0) {
            //  课程和培训中间表添加
            if(courseDao.insertTrainCourse(courseEditVo)>0) {
                for (int i = 0; i < courseEditVo.getFiles().size(); i++) {
                    List<FileVo> files = courseEditVo.getFiles();
                    FileVo file = files.get(i);
                    if (courseDao.insertList(file) > 0) {
                        if (!(courseDao.insertCourseList(courseEditVo.getId(), file) > 0)) {
                            throw new RuntimeException();
                        }

                    }
                }
                return Result.ok().message("添加成功");
            }
        }
        return Result.fail().message("添加失败");
    }

    @Transactional
    @Override
    public Result update(CourseEditVo courseEditVo) {

        if (courseDao.update(courseEditVo) >0 ){

            List<FileVo> courseFile = courseDao.findCourseFile(courseEditVo.getId());

            if(courseDao.deleteListByListId(courseFile)>0){

                if(courseDao.deleteCourseListByCourseId( courseEditVo.getId() ) > 0) {

                    List<FileVo> files = courseEditVo.getFiles();
                    for (int i = 0; i < courseEditVo.getFiles().size(); i++) {
                        FileVo file = files.get(i);

                        if (courseDao.insertList(file) > 0) {

                            if (!(courseDao.insertCourseList(courseEditVo.getId(), file) > 0)) {
                                throw new RuntimeException();
                            }

                        }
                    }
                    return Result.ok().message("更新成功");
                }
            }

//            return Result.ok(course.getId()).message("更新成功");\
            return null;
        }
        return Result.fail().message("更新失败");
    }

    @Override
    public Result deleteById(int id) {
        if (courseDao.tombstone(id) >0 ) {
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @Override
    public Result queryManagerName() {
        return Result.ok(courseDao.queryManagerName());
    }


    @Override
    public Result queryAllTrain() {
        return Result.ok(courseDao.queryAllTrain());
    }

    @Override
    public Result queryAllCondition() {
        List<TrainDto> trainDtos = courseDao.queryAllTrain();
        List<ManagerNameDto> managerNameDtos = courseDao.queryManagerName();
        List<CourseStatusDto> courseStatusDtos = courseDao.queryAllCourseStatus();
        Map<String,List> map = new LinkedHashMap<>();
        map.put("trainDtos",trainDtos);
        map.put("managerNameDtos",managerNameDtos);
        map.put("courseStatusDtos",courseStatusDtos);
        return Result.ok(map);
    }
    
    /***
     * <p>TODO: 查询课程页面的所有信息
     * @description
     * @since 2022/3/17 9:54
     * @author QinYD
     * @param id
     * @return com.woniuxy.pxxt.common.result.Result
     */

    @Override
    public Result findCourse(Integer id) {
        CourseDto courseDto = courseDao.findCourse(id);
        courseDto.setFileDomain(qiniuUtil.getFileDomain());
        List<FileVo> courseVideoDtoList  = courseDao.findCourseFile(id);
        courseDto.setFiles(courseVideoDtoList);
        return Result.ok(courseDto);
    }

    /***
     * <p>TODO: 保存用户课程的视频进度
     * @description
     * @since 2022/3/17 19:22
     * @author QinYD
     * @param courseVideoScheduleDto
     * @return com.woniuxy.pxxt.common.result.Result
     */
    @Override
    @Transactional
    public Result saveSchedule(CourseVideoScheduleDto courseVideoScheduleDto) {
        //  如果用户这个课程的视频没有记录就新增有就更新
        if(courseDao.queryCourseVideoSchedule(courseVideoScheduleDto) == null){
            if(courseDao.insertUserListSchedule(courseVideoScheduleDto)>0){
                return Result.ok().message("进度保存成功");
            }
        }else {
            if(courseDao.saveSchedule(courseVideoScheduleDto)>0){
                return Result.ok().message("进度保存成功");
            }else if (courseDao.queryCourseVideoSchedule(courseVideoScheduleDto).getIsAccomplish()==1){
                return Result.ok().message("进度保存成功");
            }
        }
        return Result.fail().message("进度保存失败");
    }
    /***
     * <p>TODO: 查询用户课程视频的进度记录
     * @description
     * @since 2022/3/17 19:25
     * @author QinYD
     * @param courseVideoScheduleDto
     * @return com.woniuxy.pxxt.common.result.Result
     */

    @Override
    public Result queryCourseVideoSchedule(CourseVideoScheduleDto courseVideoScheduleDto) {
        return Result.ok(courseDao.queryCourseVideoSchedule(courseVideoScheduleDto));
    }

    /***
     * <p>TODO: 查询用户当前课程的所有视频的进度
     * @description
     * @since 2022/3/17 23:01
     * @author QinYD
     * @param courseVideoScheduleDto
     * @return com.woniuxy.pxxt.common.result.Result
     */

    @Override
    public Result queryCourseAllVideoSchedule(CourseVideoScheduleDto courseVideoScheduleDto) {
        return Result.ok(courseDao.queryCourseAllVideoSchedule(courseVideoScheduleDto));
    }
}
